<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=0,maximum-scale=1.0" />
    <script src="http://g.tbcdn.cn/mtb/lib-flexible/0.3.4/??flexible_css.js,flexible.js"></script>
    <link rel="stylesheet" type="text/css" href="css/reset.css"/>
    <link rel="stylesheet" type="text/css" href="css/style.css"/>
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="js/public.js" type="text/javascript" charset="utf-8"></script>
    <title>扫码点餐—购物车</title>
		
</head>
<body>
	<div id="car">
		<div class="menu_header">
			<span class="current_num">当前桌号:<span class="znum"></span></span>
			<span class="people_num">
				人数:<span class="pnum"></span>人
				<i class="m_bj"></i>
			</span>
		</div>
		<div class="carTop">
			<div class="topList">
				<span class="remark">备注：</span><input id="remarks_txt" placeholder="请输入备注" >
			</div>
			<div class="topList manjian">
				<span class="full_minus">满减活动：</span><span class="activity">满<span id="condition_price"></span>减<span id="minus_price"></span></span>
			</div>
			<div class="topList">
				<span class="cutlery">餐具费：</span><span class="cutlery_fee"><span id="cutlery_fee">2</span>.00元</span>
			</div>
			<div class="topList">
				<span class="consume_sum">消费金额：¥ <span id="totalPrice"></span></span><span class="payable">应付:¥ <span id="actualPrice"></span>.00</span>
			</div>
			<div class="sum_con">
				<span class="">已加入购物车:</span><i class="del" onclick="del()"></i>
				<p><span id="totalNum"></span>件菜品</p>
			</div>
		</div>
		<div class="carCenter">
			<!--<div class="centerList">-->
				<!---->
			<!--</div>-->
		</div>
		<input type="hidden" name="count" id="count" value="" />
		<input type="hidden" name="cartlist" id="cartlist" value="" />
		<input type="hidden" name="mj_id" id="mj_id" value="" />
		<input type="hidden" name="total_price" class="total_price" value="" />
		<div class="float_con">
			<div class="continue" onclick="continu()">
				<i class="icon"></i>
				<span class="txt">继续点餐</span>
			</div>
			<div  class="promptly" onclick='goBuy()'>
				<i class="icon"></i>
				<span class="txt">立即下单</span>
			</div>
		</div>
	</div>
	
<script type="text/javascript">
 $(function () {
        (function ($) {
            $.getUrlParam = function (name) {
                var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
                var r = window.location.search.substr(1).match(reg);
                if (r != null) return unescape(r[2]);
                return null;
            }
        })
        (jQuery);
        $(".znum").html($.getUrlParam('znum'));
        $(".pnum").html($.getUrlParam('pnum'));
        znum=$(".znum").html();
        pnum=$(".pnum").html();
		
		getgoods();

    })
 	
// 	获取商品
		
 	function getgoods(){
 		console.log(1)
 		$.ajax({
        	url:url() + "index.php/Api/Car/carList",
        	dataType:"json",
        	type:"get",
        	success:function (data){
        		console.log(data);
            	if (data.state == 1) { // 状态200返回数据
        			console.log(data);
        			var carArr=data.data
        			var map = {},
				    dest = [];
					for(var i = 0; i < carArr.length; i++){
					    var ai = carArr[i];
					    if(!map[ai.category_id]){
					        dest.push({
					            id: ai.category_id,
					            name: ai.category_name,
					            data: [ai]
					        });
					        map[ai.category_id] = ai;
					    }else{
					        for(var j = 0; j < dest.length; j++){
					            var dj = dest[j];
					            if(dj.id == ai.category_id){
					                dj.data.push(ai);
					                break;
					            }
					        }
					    }
					}
				console.log(dest)
				var cart = JSON.stringify(dest)
				$('#cartlist').val(cart)
				var html1 = ''
				var count=0;
				var total_price=0;
				
		        $.each(dest, function (i, items) {
		            html1 += '<div class="centerList">';
						html1 += '<h2 class="listClassify">'+items['name']+'</h2>';
						html1 +='<ul class="listcon">';
						console.log(items.data)
						
						$.each(items.data, function (i, sitems) {
							html1 +='<li>';
							html1 +='<img class="listImg" src="'+sitems.good_pic+'"/>';
							html1 +='<div class="content">';
							html1 +='<span class="listName">'+sitems.good_name+'</span>';
							html1 +='<span class="listType"></span>';
							html1 +='<span class="listFeed">'+sitems.practice+'</span>';
							html1 +='</div>';
							html1 +='<div class="list_right">';
							html1 +='<span class="num">'+'X'+sitems.count+'</span>';
							html1 +='<span class="listPrice">'+'￥'+sitems.good_price+'</span>';
							html1 +='</div>';
							html1 +='</li>';
							count+=parseInt(sitems.count)
							total_price+=parseInt(sitems.good_price)
							})
						html1 +='</ul>';
						html1 +='</div>';
						
		        })
		        //总数量
		        $('#count').val(count)
		        $('#totalNum').text(count)
//		        餐费
				var fee=2;
				$('#cutlery_fee').text(fee)
		        //总价格
		        $('.total_price').val(total_price)
		        $('#totalPrice').text(total_price)
		        $('.carCenter').html(html1)
		        cutlery();
            	}
            }
        });
		
        
 	}
	
       function cutlery(){
       	//      餐具费 先写了默认2元一位
       		
	        var cutlery_fee=$('#cutlery_fee').text();
			console.log(cutlery_fee)
	        //总人数
	        var pnum=$(".pnum").text();
	        //总餐具费
	        var all_cutlery_fee=cutlery_fee*pnum
	        
	        $('#cutlery_fee').text(all_cutlery_fee)
	        var total_price=$('.total_price').val()
	        var total_price1=parseInt(total_price)+parseInt(all_cutlery_fee)
	        console.log(total_price)
        	$('#actualPrice').text(total_price1)
        	$('#totalPrice').text(total_price1)
        	
        	getManjian()
       }
		//继续点餐
        function continu(){
        	var cart = window.localStorage.getItem("cart")
			window.localStorage.setItem("cartlist",cart)
        	window.history.back(-1)
        }

		//满减
	function getManjian(){
		$.ajax({
            url:url() + "index.php/Api/ManJian/getManJian",
            dataType:"json",
            type:"get",
            success:function (data) {
                console.log(data.data);
  				//现在的时间戳
  				var timestamp=Math.round(new Date().getTime()/1000).toString()
  				var actualPrice=$('#actualPrice').text()
  				for (var i = 0; i < data.data.length; i++) {
  					//满减时间计算
  					if (data.data[i].end_time >= timestamp && timestamp >= data.data[i].start_time) {
						//满减价格
  						if (parseInt(data.data[i].condition_price) <= actualPrice) {
								
								$('#condition_price').text(data.data[i].condition_price)
								$('#minus_price').text(data.data[i].price)
								$('#mj_id').val(data.data[i].id)
  							}
  					} 
  				}
  				var minus_price=$('#minus_price').text()
  				console.log(actualPrice)
  				$('#actualPrice').text(actualPrice-minus_price)
  				
  				if($('#condition_price').text()==''){
  					$('.manjian').hide()
  				}else{
  					$('.manjian').show()
  				}
            }
        })
	}
	 
	
	  
        //立即下单
	function goBuy(){
       var  carCenter=$('.carCenter').html()
		if(carCenter == ''){
			alert('当前购物车为空，请前往菜单下单')
			return false;
		}else{
			var cart = JSON.parse($('#cartlist').val())
			console.log(cart)
			//满减id $('#mj_id').val(data.data[i].id)
			$.ajax({
	            url:url() + "index.php?s=/Api/DishesOrder/chooseDishes",
	            data:{'desk_number':znum,'people_number':pnum,'count':5,'list':cart,'remark':$('#remarks_txt').val(),'num':$('#totalNum').text(),'totle_price':$('#actualPrice').text(),'order_price':$('#totalPrice').text(),'discount_price':$('#minus_price').text()},
	            dataType:"json",
	            type:"post",
	            success:function (data) {
	            	if(data.state == 1){
	            		console.log($('#minus_price').text());
//	            		return false;
	            			//总金额
		                var totalPrice=$('#totalPrice').text()
		                //优惠金额
		                var minus_price=$('#minus_price').text()
		                //实际支付金额
		                var actualPrice=$('#actualPrice').text()
		                
		                window.location.href = url() + "html/meal_new/payNow.html?order_no="+data.data;

	            	}else{
	            		return false;
	            	}
	                
	            }
	        })
		}
	}
	//清空购物车
	function del(){
		$.ajax({
        	url:url() + "index.php/Api/Car/del",
        	dataType:"json",
        	type:"get",
        	success:function (data){
        		console.log(data)
        		window.location.reload()
        	}
       })
        
	}
</script>
</body>
</html>